require 'test/unit'
require 'watir'

         #~ $un="crux@eforceglobal.com"
         #~ $pwd="wafer"
         #~ $ip="10.7.10.178"
         #~ #$ie = Watir::IE.start('http://'+$ip+'/cruxWebUI/CruxHome.aspx')
         #~ $filepath="E:/Crux_Automation"
         
    class TC_03Department< Test::Unit::TestCase  
 
  #~ def test_01Login
  
         #~ require 'watir'

        #~ puts "User is going to login to the Crux Application"
        #~ sleep 5
        #~ $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtUserName$txtWaterMarkedTextBox").set $un
        #~ $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtPassword$txtWaterMarkedTextBox").set $pwd
        #~ $ie.button(:name,"ctl00$ContentPlaceHolder1$btnLogin").click
        #~ puts "Successfully logged into the Application"
        #~ sleep 10

    #~ end


def test_02AddDepartment
  
require 'xls'
require 'watir'

xlFile = XLS.new($filepath + "/Crux_Data/Add_Department.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Add Department Functionality"
puts "**************************"

myData.each do |record|
  
  $ie.link(:text,"Administration").click
  sleep 5

    $ie.link(:text,"Manage Companies").click
    sleep 5
  
    puts "Verifying existing Company : '#{record['CompanyName']}' for creating Department"   
 
    b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5
  
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkManageDepartments").click    
    sleep 8
    puts "Creating a Department '#{record['Dname']}'"
    $ie.button(:name,"ctl00$ContentPlaceHolder1$btnAddNewDepartment").click
    sleep 5    
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtDepartmentName_txtWaterMarkedTextBox').set(record['Dname'])
     $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtDepartmentDescription_txtWaterMarkedTextBox').set(record['Ddescription'])
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtDepartmentComments_txtWaterMarkedTextBox').set(record['Dcomments']) 
    
    $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click
    sleep 5   
    puts "Department: '#{record['Dname']}' created successfully"
 
    puts "Verifying the created Department: '#{record['Dname']}'"
 
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdDepartments").row_count
   
     #puts c
     
     for j in 2..c do
    u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdDepartments")[j][2].text 
    #puts u    
    if u=="#{record['Dname']}"      
      puts "Department :'#{record['Dname']}' verified successfully"
      break
    end
    end  
      break  
  end   
end
end

end

def test_03EditDepartment

require 'xls'
require 'watir'

xlFile = XLS.new($filepath + "/Crux_Data/Edit_Department.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Edit Department Functionality"
puts "**************************"

myData.each do |record|
  
  $ie.link(:text,"Administration").click
  sleep 5

  $ie.link(:text,"Manage Companies").click
   sleep 5
  
   puts "Verifying a Company : '#{record['CompanyName']}' for Department details"   
 
   b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5
     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkManageDepartments").click    
    sleep 8
  
    puts "Verifying Department : '#{record['Dname']}'for update"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdDepartments").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdDepartments")[j][2].text 
    #puts u    
    if u=="#{record['Dname']}"      
      puts "Department :'#{record['Dname']}' verified successfully"
     $ie.table(:id,"ctl00_ContentPlaceHolder1_grdDepartments")[j][5].button(:src,"http://"+$ip+"/CruxWebUI/appimages/edit_dept_details.gif").click  
      sleep 5
      puts "Updating the Department: '#{record['Dname']}' as '#{record['EDname']}'"
     $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtDepartmentName_txtWaterMarkedTextBox').set(record['EDname'])
     $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtDepartmentDescription_txtWaterMarkedTextBox').set(record['EDdescription'])
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtDepartmentComments_txtWaterMarkedTextBox').set(record['EDcomments'])     
    $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click
    sleep 5   
      puts "Department: '#{record['Dname']}' successfully updated as '#{record['EDname']}'"
 
      puts "Verifying the updated Department: '#{record['EDname']}'"
 
      rc=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdDepartments").row_count
   
     #puts c
     
     for k in 2..rc do
      eu=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdDepartments")[k][2].text 
    #puts eu  
    if eu=="#{record['EDname']}"      
      puts "Updated Department :'#{record['EDname']}' verified successfully"
      break
    end
    end      
      break
    end
    end
    break
  end 
end
end   
 
 end


def test_04DeleteDepartment

require 'xls'
require 'watir'
require 'watir/ie' 
require 'watir/contrib/enabled_popup'

xlFile = XLS.new($filepath+ "/Crux_Data/Delete_Department.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "*******************************"
puts "Delete Department Functionality"
puts "*******************************"

myData.each do |record|
  
  $ie.link(:text,"Administration").click
  sleep 5

  $ie.link(:text,"Manage Companies").click
   sleep 5
  
   puts "Verifying a Company : '#{record['CompanyName']}' for deleting Department"   
 
   b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5
     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkManageDepartments").click    
    sleep 8
  
    puts "Verifying Department : '#{record['Dname']}'for delete"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdDepartments").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdDepartments")[j][2].text 
    #puts u    
    if u=="#{record['Dname']}"      
      puts "Department :'#{record['Dname']}' verified successfully"
      puts "Deleting the department '#{record['Dname']}' "
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdDepartments")[j][5].button(:src,"http://"+$ip+"/CruxWebUI/appImages/delete.gif").click_no_wait
      sleep 5
      
      #--------------------------------------------------------------------------------------------

       hwnd = $ie.enabled_popup(5)

       if (hwnd)  #yeah! a popup
       popup = WinClicker.new
       popup.makeWindowActive(hwnd)
       popup.clickWindowsButton("Microsoft Internet Explorer", "OK", 30)
       end
 
       sleep 8    
 
      #-----------------------------------------------------------------------------------------------
      
      
      puts "Department: '#{record['Dname']}' deleted successfully "
 
      puts "Verifying the deleted Department: '#{record['Dname']}'"
 
      rc=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdDepartments").row_count
   
     #puts c
     
     for k in 2..rc do
      eu=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdDepartments")[k][2].text 
    #puts eu  
    if eu=="#{record['Dname']}"      
      flag=1
      break
    end
  end  
if flag==1
puts "Department '#{record['Dname']}' not deleted successfully"
else
  puts "Deleted Department '#{record['Dname']}' verified successfully"
  end
      break
    end
    end
    break
  end 
end
end   


 
 end



  #~ def test_05Logout
            #~ puts "Logging out from the Application"
            #~ $ie.link(:id,"ctl00_Logout_lnkLogout").click
            #~ puts "Successfully logged out from the Application"
            #~ puts "********************************"
            #~ puts "End of Departments Functionality"
            #~ puts "********************************"
            #~ sleep 5
            #~ $ie.close
  #~ end
  
  end


